package com.sky.controller.user;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.UserLoginDTO;
import com.sky.entity.Dish;
import com.sky.entity.User;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.service.UserService;
import com.sky.utils.JwtUtil;
import com.sky.vo.DishVO;
import com.sky.vo.UserLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController("UserDishController")
@Slf4j
@RequestMapping("/user/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/list")
    public Result<List<DishVO>> dishList(Integer categoryId) {
        log.info("查询菜品分类,参数为: {}", categoryId);
        //构建 Redis key
        String key = "dish_" + categoryId;
        //查询Reids中是否存在菜品数据
        List<DishVO> list = (List<DishVO>) redisTemplate.opsForValue().get(key);
        log.info("list的值为:{}", list);
        if (list != null && list.size() > 0) {
            //如果存在 直接返回  不查数据库
            return Result.success(list);
        }

        list = dishService.categoryIdByList(categoryId);

        //Redis设置
        redisTemplate.opsForValue().set(key, list);
        return Result.success(list);
    }


}
